package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.TenantProductsDAO;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.BeanUtils;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hbs.dao.VersionDAO;
import hcn.base.TenantProducts;
import hcn.base.Version;


import hcn.service.base.IVersionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by chuangye9527 on 15/9/8.
 * app版本更新服务
 */
@SsdevService(name = "versionService", value = "version", auth = false)
public class VersionService implements IVersionService {
    @Autowired
    private VersionDAO versionDAO;
    @Autowired
    private TenantProductsDAO tenantProductsDAO;


    @Override
    @RpcService
    public Version getLatestVersion(String tenantPCode) throws ControllerException {
        return versionDAO.getLatestVersion(tenantPCode);
    }

    @Override
    @RpcService
    public Version getLatestVersionWithForceUpdate(String tenantPCode, String version) throws ControllerException {
        return versionDAO.getLatestVersionWithForceUpdate(tenantPCode, version);
    }

    @Override
    @RpcService
    public Version getVersionByVersionId(String tenantPCode, String version) throws ControllerException {
        return versionDAO.getVersionByVersionId(tenantPCode, version);
    }

    @Override
    @RpcService
    public List<Version> findVersions(String tenantPCode) {
        return versionDAO.findVersions(tenantPCode);
    }

    @Override
    @RpcService
    public int addVersion(Version version) throws ControllerException {
        version.setCreateDt(new Date());
        Version exists = versionDAO.getVersionByTenantPIdAndVersion
                (version.getTenantPId(), version.getPlatform(), version.getVersion());
        if (exists != null) {
            throw new ControllerException(0, "版本重复，请修改后重新保存！");
        }
        //获取版本表中冗余字段的值
        TenantProducts tenantProducts = tenantProductsDAO.getTenantProductById(version.getTenantPId());
        if (tenantProducts != null) {
            version.setCloudId(tenantProducts.getCloudId());
            version.setProductId(tenantProducts.getProductId());
            //version.setTenantPId(tenantProducts.getTenantPId());
            version.setTenantPCode(tenantProducts.getTenantPCode());
            version.setTenantPName(tenantProducts.getTenantPName());
        }

        version = versionDAO.save(version);
        if (null != version) {
            return version.getId();
        }
        return 0;
    }

    @Override
    @RpcService
    public void update(Version version) throws ControllerException {

        Version des = versionDAO.get(version.getId());
        if (null == des) {
            return;
        }

        if (version.getProductId() == 0) {
            version.setProductId(des.getProductId());
        }
        if (version.getTenantPId() == 0) {
            version.setTenantPId(des.getTenantPId());
        }
        BeanUtils.copy(version, des);

        Version exists = versionDAO.getVersionByTenantPIdAndVersionAndId
                (des.getTenantPId(), des.getPlatform(), des.getVersion(), des.getId());
        if (exists != null) {
            throw new ControllerException(0, "版本重复，请修改后重新保存！");
        }
        //获取版本表中冗余字段的值
        TenantProducts tenantProducts = tenantProductsDAO.getTenantProductById(des.getTenantPId());

        des.setCreateDt(new Date());
        if (tenantProducts != null) {
            des.setCloudId(tenantProducts.getCloudId());
            des.setProductId(tenantProducts.getProductId());
            //des.setTenantPId(tenantProducts.getTenantPId());
            des.setTenantPCode(tenantProducts.getTenantPCode());
            des.setTenantPName(tenantProducts.getTenantPName());
        }

        versionDAO.update(des);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @Override
    @RpcService
    public Version getById(int id) {
        return versionDAO.getVersionById(id);
    }

    /**
     * 查询各个产品最新版本列表
     *
     * @param cloudId
     * @return
     */
    @Override
    @RpcService
    public List<Version> listByGroup(String cloudId) {
        return versionDAO.queryVersions(cloudId);
    }
}
